import articleModel from '../../models/article'
function updateLook_num(id){
	return new Promise(function (resolve, reject){
		articleModel.update({_id: id}, {$inc: {readingQuantity: +1}}, function (err, doc){
			if (err){
				reject('error');
			}else{
				resolve('ok');
			}
		});
	});
}
module.exports = {
    async list (ctx, next) {
        console.log('----------------获取文章列表 article/list-----------------------');
        let { keyword = '', sort = {}, tag= {}, state = '', source = '', all = '', pageindex = 1, pagesize = 10} = ctx.request.body;
        let seach = []
        let ignore = {
          toc: 0,
          source: 0
        }
        let options = {
          sort: {createAt: -1}
        }
        if (sort._id) {
          seach.push({sort: sort._id})
        }
        if (tag._id) {
          seach.push({'tags._id': {$in: [tag._id]}})
        }
        if (state.value) {
          seach.push({state: state.value})
        }
        if (source !== 'admin') {
          seach.push({state: 'publish'})
          ignore.markdown = 0
          ignore.html = 0
          ignore.state = 0
          ignore.tags = 0
          ignore.sort = 0
        } else {
          ignore.html = 0
        }
        if (all !== 'all') {
          options.limit = pagesize * 1
          options.skip = (pageindex - 1) * pagesize
        }
        try {
          let reg = new RegExp(keyword, 'i')
          let data = await ctx.findPage(articleModel,
            {$and: [
                {$or: [
                  {desc: { $regex: reg}},
                  {title: { $regex: reg}},
                  {author: { $regex: reg}}
                ]},
                ...seach
             ]},
             ignore,
             options);
            ctx.resResult(data)
        }catch (e){
            console.log(e)
            ctx.resResultError(e)
        }
    },

    async asideList (ctx, next) {
        console.log('----------------获取浏览量最多的十篇文章 article/asideList-----------------------');
        let sort = ctx.request.query.sort;
        console.log(sort)
        let ignore = {
          toc: 0,
          source: 0,
          markdown: 0,
          html: 0,
          state: 0,
          tags: 0,
          sort: 0
        }
        try {
          let data = await ctx.find(articleModel, {state: 'publish'}, ignore, {limit: 10, sort: {readingQuantity: -1}});
          console.log(data)
          ctx.resResult(data)
        }catch (e){
          console.log(e)
          ctx.resResultError(e)
        }
    },
    async thisSort (ctx, next) {
        console.log('----------------获取this分类列表 article/thisSort-----------------------');
        let sort = ctx.request.query.sort;
        console.log(sort)
        let ignore = {
          toc: 0,
          source: 0,
          markdown: 0,
          html: 0,
          state: 0,
          tags: 0,
          sort: 0
        }
        try {
          let data = await ctx.find(articleModel, {'sort': sort, state: 'publish'}, ignore, {sort: {createAt: -1}});
          ctx.resResult(data)
        }catch (e){
          console.log(e)
          ctx.resResultError(e)
        }
    },
    async thisTag (ctx, next) {
        console.log('----------------获取this标签列表 article/thisTag-----------------------');
        let tag = ctx.request.query.tag;
        let ignore = {
          toc: 0,
          source: 0,
          markdown: 0,
          html: 0,
          state: 0,
          tags: 0,
          sort: 0
        }
        try {
          let data = await ctx.find(articleModel, {'tags._id': {$in: [tag]}, state: 'publish'}, ignore, {sort: {createAt: -1}});
          ctx.resResult(data)
        }catch (e){
          console.log(e)
          ctx.resResultError(e)
        }
    },
    async aroundThis (ctx, next) {
      console.log('----------------获取上下两篇文章接口 article/aroundThis-----------------------');
      let id = ctx.request.query.id;
      let ignore = {
        title: 1,
        _id: 1
      }
      try {
          let data = await ctx.findOne(articleModel, {_id: id, state: 'publish'})
          console.log(data.createAt)
          let datalt = await ctx.find(articleModel, {createAt: {"$lt": data.createAt}, state: 'publish'}, ignore, {limit: 1, sort: {createAt: -1}})
          let datagt = await ctx.find(articleModel, {createAt: {"$gt": data.createAt}, state: 'publish'}, ignore, {limit: 1, sort: {createAt: 1}})
          // let datalt = await ctx.find(articleModel, {_id: {"$lt": id}, state: 'publish'}, ignore, {limit: 1, sort: {createAt: -1}})
          // let datagt = await ctx.find(articleModel, {_id: {"$gt": id}, state: 'publish'}, ignore, {limit: 1, sort: {createAt: 1}})
          if (datagt.length === 0) {
            datagt.push('')
          }
          if (datalt.length === 0) {
            datalt.push('')
          }
          ctx.resResult([...datagt, ...datalt], 'success')
          // }
      }catch(e) {
          ctx.resResultError(e)
      }
    },
    async article (ctx, next) {
      console.log('----------------获取当前文章接口 article/getArticle-----------------------');
      let id = ctx.request.query.id;
      let ignore = {
        toc: 0,
        source: 0,
        markdown: 0,
        state: 0,
        source: 0,
      }
      try {
        await updateLook_num(id)
        let data = await ctx.findOne(articleModel, {_id: id}, ignore)
        if (!data) {
            ctx.resResultError('无此文章')
        } else {
            ctx.resResult(data, 'success')
        }
      }catch(e) {
          ctx.resResultError(e)
      }
    },
    async add (ctx, next) {
        console.log('----------------添加博客 blog/add-----------------------');
        let paramsData = ctx.request.body;
        try {
            let data = await ctx.findOne(articleModel, {title: paramsData.title})
            if (data) {
                ctx.resResultError('数据已经存在, 请重新添加!')
            } else {
                // paramsData.html = marked(paramsData.html);
                let data = await ctx.add(articleModel, paramsData);
                ctx.resResult({}, '添加成功!')
            }
        }catch(e) {
            ctx.resResultError(e)
        }
    },
    async update (ctx, next) {
        console.log('----------------更新博客 blog/update-----------------------');
        let paramsData = ctx.request.body;
        try {
            // paramsData.html = marked(paramsData.html);
            let data = await ctx.update(articleModel, {_id: paramsData.id}, paramsData)
            ctx.resResult({}, '更新成功!')
        }catch(e) {
            if (e === '暂无数据') {
                ctx.resResultError(e)
            }
        }
    },
    async del (ctx, next) {
        console.log('----------------删除博客 blog/del-----------------------');
        let id = ctx.request.query.id
        try {
            ctx.remove(articleModel, {_id: id})
            ctx.resResult({}, '删除成功!')
        }catch(e){
            ctx.resResultError(e)
        }
    }
}
